package com.bj58.groupbuy.action.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import com.bj58.groupbuy.action.db.CommonDao;
import com.bj58.groupbuy.action.db.DBConfig;
import com.bj58.groupbuy.action.model.Menu;
import com.bj58.groupbuy.action.service.MenuService;
import com.bj58.groupbuy.action.util.MyStore;
import com.bj58.groupbuy.service.impl.BaseService;

public class MenuServiceImpl  extends BaseService<Menu>implements MenuService {
	private CommonDao comDao = new CommonDao();
	
	@Override
	public List<Menu> getMenuListByFunctions(String function) throws Exception {
//		String where = "id("+function+")";
//		return  getGenericDao().find(Menu.class, "*", where+"&isshow=1&id<555", "");
		return comDao.getAllColsByVo("select * from menu where isshow=1 and id in(" + function+") ", Menu.class, DBConfig.db_68_bi_sys.getIntType());
	}

	@Override
	public List<Menu> getAllMenu() throws Exception {
//		return getGenericDao().find(Menu.class, "*", "isshow=1&id<555", "");
		return comDao.getAllColsByVo("select * from menu where isshow=1  ", Menu.class, DBConfig.db_68_bi_sys.getIntType());
	}

	@Override
	public TreeMap<String, List<Menu>> getMenuMap() throws Exception {
		List<Menu>  menuList = getAllMenu();  //获取所有权限节点列表
		TreeMap<String, List<Menu>> menuMap = new TreeMap<String, List<Menu>>();
		for (Menu menu : menuList) {
			if (menu.getId() > 1000) {
				continue;
			}
			String cate1 = menu.getCate1();
			String cate2 = menu.getCate2();
			if(null == cate2 || "".equals(cate2)){
				cate2 = "无分类";
			}
			String key = cate1+"-"+cate2;
			List<Menu> list = new ArrayList<Menu>();
			if (menuMap.containsKey(key))
				list = menuMap.get(key);
			list.add(menu);
			menuMap.put(key, list);
		}
		
		return menuMap;
	}

	@Override
	public Menu findByURL(String url) throws Exception {
		List<Menu> rsList = comDao.getAllColsByVo("select * from menu where isshow=1 and action='" + url+"' ", Menu.class, DBConfig.db_68_bi_sys.getIntType());
		if(null == rsList || rsList.isEmpty() || rsList.size() > 1){
			return null;
		}else{
			return rsList.get(0);
		}
		
	}

	@Override
	public List<Menu> getListByCate1(String cate1,String fun) throws Exception {
		String sql = "select * from menu where isshow=1 and cate1='" + cate1+"' ";
		if(!fun.equals(MyStore.admin)){
			sql = sql + " and id in(" + fun +") ";
		}
		List<Menu> rsList = comDao.getAllColsByVo(sql , Menu.class, DBConfig.db_68_bi_sys.getIntType());
		if(null == rsList || rsList.isEmpty()){
			return null;
		}else{
			return rsList;
		}
	}


}
